<?php

header ('Content-type: text/html; charset=UTF-8'); 

//header('Expires: '.gmdate('D, d M Y H:i:s \G\M\T', time() - 3600));


?>

<script src="system.js"></script>
<script src="md5-min.js"></script>
<script src="hmac-md5.js"></script>



<script language="javascript" type="text/javascript">

			
	function submitbutton(pressbutton) 
	{
		//var form = document.adminForm;
	
		switch( pressbutton )
		{
			
			case	"send":
			{
				
					
					//form.btnsave.disabled = true;					
					//form.task.value=pressbutton;

					
					var oObject = {} ;
					
					oObject["id"] = getInputValueElementId ("id" );
					oObject["metadata"] = encodeURIComponent(getInputValueElementId( "metadata" ));
					oObject["component"] = "etl";
					oObject["controller"] = "metadata";
					oObject["task"] = "save";
					
					var jsonVales	=	convertToJSON(oObject);
					
					parameters	=	encodeURIComponent(
											convertToStringJSON(jsonVales));
					
					var method='PUT';
						
					httpRequest ( method,
									url_API,
									parameters,
									'retornoViewMetadata');
					
			
						
					return false;
				
				
			}break;
			
			default:
			{
				
			}
		}	
		
	}


function retornoViewMetadata(jsonObj){
		
	if( jsonObj.status == 1){
		
		//alert( jsonObj.data );
		window.location.href='view_list.php';
			
	}else{
		
		alert( jsonObj.data );
	}

}





var json_metadata;

function returnMetadata(jsonObj){

	var obj_json = JSON.parse(jsonObj.data);

	document.getElementById("id").value = obj_json[0].id;
	document.getElementById("metadata").value = obj_json[0].metadata;
	
	metadata = obj_json[0].metadata;
	
	if( metadata ==="null" 
		|| metadata===null 
		|| metadata==="" 
		|| typeof metadata === "undefined"  ){
			
		json_metadata = JSON.parse("[]"); 
	}else{
		json_metadata = JSON.parse(obj_json[0].metadata); 
	}
	
}



function getItemViewMetadata(id) 
{

	if( id!=''){

		var oObject = {} ;
						
		oObject["id"] = id;
		oObject["component"] = "etl";
		oObject["controller"] = "metadata";
		oObject["task"] = "getItem";

		var jsonVales	=	convertToJSON(oObject);

		parameters	=	encodeURIComponent(
								convertToStringJSON(jsonVales));

		var method='GET';

		httpRequest ( method,
						url_API,
						parameters,
						'returnMetadata');
	}

					
}


function updateJSONMetadata(column, column_type, column_detail){
	
	
	var metadataExist=false;
	
	//alert(json_metadata.length);
	
	for (i=0; i < json_metadata.length; i++){

		if(json_metadata[i].column){
			
			if( json_metadata[i].column == column.value ){
			
				json_metadata[i].type = column_type.options[column_type.selectedIndex].text;
				json_metadata[i].detail = encodeURIComponent(column_detail.value);
				metadataExist=true;
				break;
			}
			
			
		}else{
			
			/*var oObject = {} ;
					
			oObject["column"] =column.value;
			oObject["type"] = column_type.options[column_type.selectedIndex].text;
			oObject["detail"] = encodeURIComponent(column_detail.value);
		
			json_metadata	=	convertToJSON(oObject);
					
					
			//json_metadata[i].column=column.value;
			//json_metadata[i].type = column_type.options[column_type.selectedIndex].text;
			//json_metadata[i].detail = encodeURIComponent(column_detail.value);
			metadataExist=true;
			break;*/
			
		}

		
			

	}


	if( metadataExist == false){
		
		//alert( json_metadata.length );
		var oObject = {} ;
					
			oObject["column"] =column.value;
			oObject["type"] = column_type.options[column_type.selectedIndex].text;
			oObject["detail"] = encodeURIComponent(column_detail.value);
		
			json_metadata[json_metadata.length]	=	convertToJSON(oObject);
			
			
		//json_metadata[json_metadata.length].column = column.value;
		//json_metadata[json_metadata.length].type = column_type.options[column_type.selectedIndex].text;
		//json_metadata[json_metadata.length].detail = encodeURIComponent(column_detail.value);
	}
	
	
	document.getElementById("metadata").value = convertToStringJSON(json_metadata);
	
}



function enriquecerColuna(coluna){
	
	var select_ = document.getElementById("column_table");
	
	if(select_.options[select_.selectedIndex].value=='-1'){
		return false;
	}
	
	
	var metadataExist=false;
	
	for (i=0; i < json_metadata.length; i++){

		if( json_metadata[i].column == coluna ){
			
			document.getElementById("column").value = coluna;
			document.getElementById("column_type").selectedIndex = json_metadata[i].type;
			document.getElementById("column_detail").value = decodeURIComponent(json_metadata[i].detail);
			metadataExist=true;
		}
			//alert(json_metadata[i].detail);

	}
	
	
	if( metadataExist == false){
		//alert("falso");
		document.getElementById("column").value = coluna;
		//document.getElementById("column_type").selectedIndex = json_metadata[i].type;
		document.getElementById("column_detail").value = '';
	}
	

}


function returnMetadataColumnsSELECT(jsonObj){
	

	if( jsonObj.status == 1){
		

		var grid_view_html="";
		var obj_json = JSON.parse(jsonObj.data);
		var i;
		
		grid_view_html ="<select id='column_table' name='column_table' onchange=\"javascript:enriquecerColuna(this.options[this.selectedIndex].text)\">";
		grid_view_html = grid_view_html + "<option value='-1'>-- Selecione --</option>";
		//grid_view_html = grid_view_html +"<td>Colunas</td>";
		//grid_view_html = grid_view_html +"</tr>";
		
		
		for (i=0; i < obj_json.length; i++){


		    //grid_view_html = grid_view_html +"<tr>";
		    		    		
		    for (var key in obj_json[i]) {
			    	
				grid_view_html = grid_view_html +"<option value='"+obj_json[i][key]+"'>"+obj_json[i][key]+"</option>";
			    	
			}

		    //grid_view_html = grid_view_html +"</select>";

		}


		document.getElementById("grid_view").innerHTML = ""+grid_view_html+"</select>";
		//document.getElementById("grid_view").innerHTML = "<table border=1><tr><td>#</td><td>id</td><td>nome</td><td>table</td><td>host</td><td>detalhe</td><td>criado</td></tr>"+grid_view_html+"</table>";

		
	}else{
		
		alert(jsonObj.data);
		
		//if(jsonObj.notification==25){
		//	window.location.href='index.php';
		//}
	}
	
	
	//var obj_json = JSON.parse(jsonObj.data);
	//alert(jsonObj.data);
	//document.getElementById("id").value = obj_json[0].id;
	//document.getElementById("metadata").value = obj_json[0].metadata;
	
}






function returnMetadataColumnsTABLE(jsonObj){
	

	if( jsonObj.status == 1){
		

		var grid_view_html="";
		var obj_json = JSON.parse(jsonObj.data);
		var i;
		
		grid_view_html ="<tr>";
		grid_view_html = grid_view_html +"<td>Colunas</td>";
		grid_view_html = grid_view_html +"</tr>";
		
		
		for (i=0; i < obj_json.length; i++){


		    grid_view_html = grid_view_html +"<tr>";
		    		    		
		    for (var key in obj_json[i]) {
			    	
				grid_view_html = grid_view_html +"<td><a href=\"javascript:enriquecerColuna('"+obj_json[i][key]+"')\">"+obj_json[i][key]+"</a></td>";
			    	
			}

		    grid_view_html = grid_view_html +"</tr>";

		}


		document.getElementById("grid_view").innerHTML = "<table border=1>"+grid_view_html+"</table>";
		//document.getElementById("grid_view").innerHTML = "<table border=1><tr><td>#</td><td>id</td><td>nome</td><td>table</td><td>host</td><td>detalhe</td><td>criado</td></tr>"+grid_view_html+"</table>";

		
	}else{
		
		alert(jsonObj.data);
		
		//if(jsonObj.notification==25){
		//	window.location.href='index.php';
		//}
	}
	
	
	//var obj_json = JSON.parse(jsonObj.data);
	//alert(jsonObj.data);
	//document.getElementById("id").value = obj_json[0].id;
	//document.getElementById("metadata").value = obj_json[0].metadata;
	
}



function getItemViewColumnsTable(key) 
{

	if( key!=''){

		var oObject = {} ;
						
		oObject["key"] = key;//hex_md5(id);
		oObject["component"] = "etl";
		oObject["controller"] = "view";
		oObject["task"] = "getColumnsKey";

		var jsonVales	=	convertToJSON(oObject);

		parameters	=	encodeURIComponent(
								convertToStringJSON(jsonVales));

		var method='GET';

		httpRequest ( method,
						url_API,
						parameters,
						'returnMetadataColumnsSELECT');
	}

					
}

</script>

<h1>Editar Metadados</h1>
<div id="text-result-view" style="visibility:hidden;width:1px;height:1px;"></div>
<div id="text-result-hidden" style="visibility:hidden;width:1px;height:1px;"></div>
<div id="text-result-hidden-username" style="visibility:hidden;width:1px;height:1px;"></div>

<div id="text-result">Area restria aos usuarios autenticados. voce esta logado como <div id="username-id"></div>.<a href="#" onclick="javascript:logout();">Clique aqui para Logout</a></div>
 
<br>

<a href="home_authenticated.php">Página inicial</a><br>
<a href="view_list.php">Lista de Visões</a><br>


<br><br>

<table>
	<tr>
		<td valign="top">
			
			<div id="grid_view"></div>

		</td>
	</tr>
	<tr>
		<td valign="top">
			
<form class="cmxform" action="#" method="GET">	
	
	<div id="label">Campo</div> <div id="field"><input type="text" name="column" id="column" value='' readonly=""></div>
	
	<div id="label">Tipo do Campo</div> <div id="field">
		<select name="column_type" id="column_type">
			<option value="Número/Inteiro">Número/Inteiro</option>
			<option value="Número/Decimal">Número/Decimal</option>
			<option value="Texto">Texto</option>
			<option value="Data/Hora">Data/Hora</option>
		</select>
		
		
	</div>
	
	
	<div id="label">Descrição do Campo</div> <div id="field">
		<textarea id="column_detail" name="column_detail" style="width:500px;height:150px;"></textarea>		
	</div>
	
	
	<input type="button" name="enviar" value="Salvar" onclick="javascript:updateJSONMetadata(document.getElementById('column'),document.getElementById('column_type'),document.getElementById('column_detail'));"/>

	
	
</form>

		</td>
	</tr>
	
</table>




<form class="cmxform" action="" method="GET">	
	<input type="hidden" name="id" id="id" value=''>
	
	<div id="label">metadados</div> <div id="field">
		<textarea id="metadata" name="metadata" style="width:500px;height:150px;"  ></textarea>		
	</div>
	
	
	<input type="button" name="enviar" value="Salvar" onclick="javascript:submitbutton('send');"/>

	<input type="button" name="cancelar" value="Cancelar" onclick="javascript:window.location.href='view_list.php';"/>
	
</form>


<script>
getUsername();
getItemViewMetadata(getURIParameter('id'));
getItemViewColumnsTable( hex_md5(getURIParameter('id')) );
</script>